import router from '../router';
// import { defineStore } from 'pinia';
// import { store } from '@/store';
import { useUserStoreWithOut } from '@/store/modules/user';

const userStore = useUserStoreWithOut();
export type icon = 'none' | 'success' | 'loading' | 'error' | 'fail' | 'exception' | undefined;
//常用方法集合
export default class useHelperStore {
  // export const useHelperStore = defineStore({
  /**
   * toast提示
   */
  toast(title, duration = 3000, mask = false, icon): void {
    if (Boolean(title) === false) {
      return;
    }

    uni.showToast({
      title,
      duration,
      mask,
      icon,
    });
  }
  /**
   * 返回登录页面
   */
  async backToLogin() {
    // 存当前页面的地址
    const currentPage = getCurrentPages()[getCurrentPages().length - 1];
    const params: any = {};
    console.log('currentPage', currentPage);
    // params.route = `/${currentPage.$vm.__route__}`;
    // params.query = currentPage.$vm.$mp && currentPage.$vm.$mp.query;
    uni.setStorageSync('backToPage', JSON.stringify(params));
    await userStore.logout();
    router.push({ route: '/pages/village/index' });
  }
  /**
   * 返回上一页携带参数
   */
  prePage(index) {
    let pages = getCurrentPages();
    let prePage = pages[pages.length - (index || 2)];
    // #ifdef H5
    return prePage;
    // #endif
    // return prePage.$vm;
  }
  /**
   * 开发环境全局打印日志
   * @param {Object} title
   */
  log(title) {
    if (process.env.NODE_ENV === 'development' && Boolean(title) === true) {
      console.log(JSON.stringify(title));
    }
  }
  /**
   * 异步获取设备信息
   */
  getInfoAsync() {
    return new Promise((resolve, reject) => {
      plus.device.getInfo({
        success(e) {
          resolve(e);
        },
        fail(e) {
          reject(e.message);
        },
      });
    });
  }
  /**
   * 安卓10不支持IMEI,则获取OAID
   */
  getOaidAsync() {
    return new Promise((resolve, reject) => {
      plus.device.getOAID({
        success(e) {
          resolve(e);
        },
        fail(e) {
          reject(e.message);
        },
      });
    });
  }
  /**
   * 获取一个随机数
   * @param {Object} min
   * @param {Object} max
   */
  random(min, max) {
    switch (arguments.length) {
      case 1:
        return parseInt((Math.random() * min + 1, 10).toString());
        break;
      case 2:
        return parseInt(Math.random() * (max - min + 1) + min, 10);
        break;
      default:
        return 0;
        break;
    }
  }
  /**
   * 获取ios的IDFA
   */
  getIdfa() {
    let idfa = '';
    try {
      if ('iOS' == plus.os.name) {
        let manager = plus.ios.invoke('ASIdentifierManager', 'sharedManager');
        if (plus.ios.invoke(manager, 'isAdvertisingTrackingEnabled')) {
          let identifier = plus.ios.invoke(manager, 'advertisingIdentifier');
          idfa = plus.ios.invoke(identifier, 'UUIDString');
          plus.ios.deleteObject(identifier);
        }
        plus.ios.deleteObject(manager);
      }
    } catch (e) {
      console.error('获取idfa失败');
    }
    return idfa;
  }
  /*
   * obj 转 params字符串参数
   * 例子：{a:1,b:2} => a=1&b=2
   */
  objParseParam(obj) {
    let paramsStr = '';
    if (obj instanceof Array) return paramsStr;
    if (!(obj instanceof Object)) return paramsStr;
    for (let key in obj) {
      paramsStr += `${key}=${obj[key]}&`;
    }
    return paramsStr.substring(0, paramsStr.length - 1);
  }
  /*
   * obj 转 路由地址带参数
   * 例子：{a:1,b:2} => /pages/village/index?a=1&b=2
   */
  objParseUrlAndParam(path, obj) {
    let url = path || '/';
    let paramsStr = '';
    const _this: any = this;
    if (obj instanceof Array) return url;
    if (!(obj instanceof Object)) return url;
    paramsStr = _this.objParseParam(obj);
    paramsStr && (url += '?');
    url += paramsStr;
    return url;
  }
  /*
   * 获取url字符串参数
   */
  getRequestParameters(locationhref) {
    let href = locationhref || '';
    let theRequest = new Object();
    let str = href.split('?')[1];
    if (str != undefined) {
      let strs = str.split('&');
      for (let i = 0; i < strs.length; i++) {
        theRequest[strs[i].split('=')[0]] = strs[i].split('=')[1];
      }
    }
    return theRequest;
  }
  /**
   * 加密字符串
   */
  strEncode(str) {
    const key = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    let l = key.length;
    let a = key.split('');
    let s = '',
      b,
      b1,
      b2,
      b3;
    for (let i = 0; i < str.length; i++) {
      b = str.charCodeAt(i);
      b1 = b % l;
      b = (b - b1) / l;
      b2 = b % l;
      b = (b - b2) / l;
      b3 = b % l;
      s += a[b3] + a[b2] + a[b1];
    }
    return s;
  }
  /**
   * 解密字符串
   */
  // strDecode(str) {
  //   const key = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  //   let l = key.length;
  //   let b,
  //     b1,
  //     b2,
  //     b3,
  //     d = 0,
  //     s;
  //   s = new Array(Math.floor(str.length / 3));
  //   b = s.length;
  //   for (let i = 0; i < b; i++) {
  //     b1 = key.indexOf(str.charAt(d));
  //     d++;
  //     b2 = key.indexOf(str.charAt(d));
  //     d++;
  //     b3 = key.indexOf(str.charAt(d));
  //     d++;
  //     s[i] = b1 * l * l + b2 * l + b3;
  //   }
  //   b = eval(String.fromCharCode + '(' + s.join(',') + ')');
  //   return b;
  // },
  /**
   * 比较版本号
   */
  compareVersion(reqV, curV) {
    if (curV && reqV) {
      let arr1 = curV.split('.'),
        arr2 = reqV.split('.');
      let minLength = Math.min(arr1.length, arr2.length),
        position = 0,
        diff = 0;
      while (
        position < minLength &&
        (diff = parseInt(arr1[position]) - parseInt(arr2[position])) == 0
      ) {
        position++;
      }
      diff = diff != 0 ? diff : arr1.length - arr2.length;
      if (diff > 0) {
        if (position == minLength - 1) {
          return 1;
        } else {
          return 2;
        }
      } else {
        return 0;
      }
    } else {
      return 0;
    }
  }
  /**
   * H5复制
   */
  h5Copy(content) {
    let textarea: any = document.createElement('textarea');
    textarea.value = content;
    textarea.readOnly = 'readOnly';
    document.body.appendChild(textarea);
    textarea.select(); // 选择对象
    textarea.setSelectionRange(0, content.length); //核心
    let result = document.execCommand('Copy'); // 执行浏览器复制命令
    textarea.remove();
    const msg = result ? '复制成功' : '复制失败';
    const _this: any = this;
    _this.toast(msg, 300, false, 'none');
  }

  // 去掉字符串中的空格
  trim(str) {
    if (!str) {
      return '';
    }
    return str.replace(/\s*/g, '');
  }

  // 判断两个对象是否相同
  isObjectValueEqual(x: any, y: any): boolean {
    // 指向同一内存时
    if (x === y) {
      return true;
    } else if (typeof x == 'object' && x != null && typeof y == 'object' && y != null) {
      if (Object.keys(x).length != Object.keys(y).length) return false;
      const _this: any = this;
      for (var prop in x) {
        if (y.hasOwnProperty(prop)) {
          if (!_this.isObjectValueEqual(x[prop], y[prop])) return false;
        } else return false;
      }

      return true;
    } else return false;
  }
  // });
}

// export function useHelperWithOut() {
//   return useHelperStore(store);
// }
